﻿using DevExpress.XtraEditors.Controls;
using RecruitStudents.Client.BLL;
using RecruitStudents.Utility;
using RecruitStudents.Utility.Const;
using RecruitStudents.Utility.Helper;
using RecruitStudents.Utility.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RecruitStudents.Client.Base
{
    public partial class Base_User_Option : BaseForm
    {
        string UserID = string.Empty;
        ServiceBase service = new ServiceBase();
        TB_Sys_User model = null;
        public Base_User_Option(string strUserID)
        {
            InitializeComponent();
            UserID = strUserID;
            this.cmbUserType.Properties.NullText = "";
            this.cmbUserType.Properties.Columns.Add(new LookUpColumnInfo("Key", 30, "类型代码"));
            this.cmbUserType.Properties.Columns.Add(new LookUpColumnInfo("Value", 55, "类型名称"));
            this.cmbUserType.Properties.DataSource = ConstAppConfig.DicUserType.ToList();
            this.cmbUserType.Properties.DisplayMember = "Value";
            this.cmbUserType.Properties.ValueMember = "Key";

            this.cmbRole.Properties.NullText = "";
            this.cmbRole.Properties.Columns.Add(new LookUpColumnInfo("RoleID", 30, "角色ID"));
            this.cmbRole.Properties.Columns.Add(new LookUpColumnInfo("RoleName", 55, "角色名称"));
 
            using (DataTable dt = service.GetRoleTable(string.Empty, "1"))
            {
                this.cmbRole.Properties.DataSource = dt;
                this.cmbRole.Properties.DisplayMember = "RoleName";
                this.cmbRole.Properties.ValueMember = "RoleID";
                this.cmbRole.Properties.Columns[0].Visible = false;
            }

            if (UserID.ToSafeString().Length > 0)
            {
                model = service.GetUserModel(UserID);
                if (model != null)
                {
                    model.Attach();
                    txtLoginName.Text = model.LoginName;
                    txtName.Text = model.Name;
                    txtPassword.Text = model.Password.ToSafeDecrypt();
                    cmbUserType.EditValue = model.Type;
                    if (model.IsActive == "1")
                    {
                        chkIsActive.Checked = true;
                    }
                    else
                    {
                        chkIsActive.Checked = false;
                    }
                    cmbRole.EditValue = model.RoleID;
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtName.Text.ToSafeString().Length == 0)
            {
                DevHelper.ShowInfoMessageBox("请先输入名称!");
                this.txtName.Focus();
                return;
            }
            if (txtLoginName.Text.ToSafeString().Length == 0)
            {
                DevHelper.ShowInfoMessageBox("请先输入登陆名!");
                this.txtLoginName.Focus();
                return;
            }
            if (txtPassword.Text.ToSafeString().Length == 0)
            {
                DevHelper.ShowInfoMessageBox("请先输入密码!");
                this.txtPassword.Focus();
                return;
            }
            if (cmbUserType.Text == "" || cmbUserType.EditValue == null || cmbUserType.EditValue.ToSafeString().Length == 0)
            {
                DevHelper.ShowInfoMessageBox("请先选择类型!");
                this.cmbUserType.Focus();
                return;
            }
            if (model == null)
            {
                model = new TB_Sys_User();
                model.CreateDate = DateTime.Now;
            }
            model.IsActive = chkIsActive.Checked ? "1" : "0";
            model.LoginName = txtLoginName.Text;
            model.Name = txtName.Text;
            model.Password = txtPassword.Text.ToSafeEncrypt();
            model.Type = cmbUserType.EditValue.ToSafeString();
            if (cmbRole.Text == "" || cmbRole.EditValue == null || cmbRole.EditValue.ToSafeString().Length == 0)
            {
                model.RoleID = string.Empty;
            }
            else
            {
                model.RoleID = cmbRole.EditValue.ToSafeString();
            }
            if (service.SaveUser(model))
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
